// pages/accounts/add/add.js
const app = getApp();
const screenservice = require("../../../apiservice/screen/screenservice");
const screencustomerservice = require("../../../apiservice/screen/screencustomerservice");
const envVersion = app.globalData.envVersion;
const jsondataConfig = require("../jsondata");
const util = require("../../../utils/util.js");
Page({

	/** 
	 * 页面的初始数据
	 */
	data: {
		screenId: "",
		templateid: "",
		new_screenteamId: "",
		genderIndex: 0,
		isOpen: false,
		opName: "更多联系方式",
		desease: jsondataConfig.desease,
		deseaseIndex: 0,
		lead: {
			new_patientsource: 3,
		},
		SCOBJ: {},
		new_screenitemId: '',
		samenameqty: '',
		IsShowtip: false,
		skipphotoStatus: 0,
		ownerid: "",
        envVersion: envVersion
	},

	changeDesease: function(e) {
		var index = e.detail.value;
		this.setData({
			deseaseIndex: index,
		});
		if (index != 0) {
			this.setData({
				['lead.new_disease']: this.data.desease[index].id
			});
		}
	},
	onNameChange: function(e) {
		this.setData({
			['lead.lastname']: e.detail
		});

	},
	onNameBlur: function(e) {
		let scid = this.data.screenId;
		var that = this;
		screencustomerservice.GetSameNameQuantity(scid, e.detail.value).then(function(res) {
			console.log(res.data);
			that.setData({
				IsShowtip: true,
				samenameqty: "+" + res.data
			})
		});
	},
	onMobilephoneChange: function(e) {
		if (!util.checkNum(e.detail)) {
			util.shouTips();
			this.setData({
				['lead.address1_telephone1']: '',
				['lead.new_phone']: ''
			})
		} else {
			this.setData({
				['lead.address1_telephone1']: e.detail,
				['lead.new_phone']: e.detail
			});
		}
	},
	onMobilephone2Change: function(e) {
		if (!util.checkNum(e.detail)) {
			util.shouTips();
			this.setData({
				['lead.new_mobile']: ''
			})
		} else {
			this.setData({
				['lead.new_mobile']: e.detail
			});
		}

	},
	onTelephoneChange: function(e) {
		if (!util.checkNum(e.detail)) {
			util.shouTips();
			this.setData({
				['lead.telephone2']: ''
			})
		} else {
			this.setData({
				['lead.telephone2']: e.detail
			});
		}
	},
	onTelephone2Change: function(e) {
		if (!util.checkNum(e.detail)) {
			util.shouTips();
			this.setData({
				['lead.address1_telephone2']: ''
			})
		} else {
			this.setData({
				['lead.address1_telephone2']: e.detail
			});
		}

	},
	onAgeChange: function(e) {

		if (!util.checkNum(e.detail)) {
			util.shouTips();
			this.setData({
				['lead.new_age']: ''
			})
		} else {
			this.setData({
				['lead.new_age']: e.detail
			});
		}
	},
	onGenderChange: function(e) {
		//console.log(e);
		this.setData({
			genderIndex: e.detail.index
		});
	},
	onIdCardNoChange: function(e) {
		this.setData({
			['lead.new_citizenid']: e.detail
		});
		var idcard_patter =
			/^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
		var idnum = e.detail;
		if (idcard_patter.test(idnum)) {
			var born = idnum.substring(6, 10) + "-" + idnum.substring(10, 12) + "-" + idnum.substring(12,
				14);
			var birthYear = parseInt(idnum.substring(6, 10));
			var currYear = new Date().getFullYear();
			var age = currYear - birthYear;
			this.setData({
				['lead.new_birthdate']: born,
				['lead.new_age']: age
			})
		}
	},
	onIdCardBlur: function(e) {
		var idnum = e.detail.value;
		var sex = idnum.substring(idnum.length - 2, idnum.length - 1);
		if (sex % 2 > 0) { //奇数
			this.setData({
				genderIndex: 0
			});
		} else {
			this.setData({
				genderIndex: 1
			});
		}
	},
	onHomeAddrChange: function(e) {
		this.setData({
			['lead.address1_name']: e.detail
		});
	},
	onEmailChange: function(e) {
		this.setData({
			['lead.emailaddress1']: e.detail
		});
	},
	onQQChange: function(e) {
		if (!util.checkNum(e.detail)) {
			util.shouTips();
			this.setData({
				['lead.new_qq']: ''
			})
		} else {
			this.setData({
				['lead.new_qq']: e.detail
			});
		}

	},
	onWechatChange: function(e) {
		this.setData({
			['lead.new_wechat']: e.detail
		});
	},
	//['lead.new_age']: age,
	scanSuccess: function(e) {
		//debugger;
		console.log(e);
		let detail = e.detail;
		var idnum = detail.id.text;
		var birthYear = parseInt(idnum.substring(6, 10));
		var currYear = new Date().getFullYear();
		let age = currYear - birthYear;
		//let age = util.getAgeBybirthDay(detail.birthday);
		//console.log(e, age);
		this.setData({
			['lead.new_citizenid']: detail.id.text,
			['lead.lastname']: detail.name.text,
			['lead.address1_name']: detail.address.text,
			['lead.new_age']: age,
			genderIndex: (detail.gender.text == "女" ? 1 : 0)
		})
	},

	setPersonIdCard:function(idCard){
			
		let { birthDate, gender, address, nationality, name, idNumber } = idCard;

	

		debugger;
		let birthYear = parseInt(birthDate.substring(0, 4));
		let currYear = new Date().getFullYear();
		let age = currYear - birthYear;
		

		this.setData({
			['lead.new_citizenid']: idNumber,
			['lead.lastname']: name,
			['lead.address1_name']: address,
			['lead.new_age']: age,
			genderIndex: (gender == "女" ? 1 : 0)
		})
		
	},

	onCamera:function(){
       wx.navigateTo({
		   url: "/pages/camera/camera"
	   });
	},

	onSubmit: function() {
		//字段验证
		var flag = this.checkPhoneAndIDCardAndName(this.data.lead);
		if (!flag) {
			return;
		}
		this.SaveLeadAccount();
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		if (options.new_screenId) {
			this.setData({
				screenId: options.new_screenId,
			});
		}
		if (options.new_screenteamId) {
			this.setData({
				new_screenteamid: options.new_screenteamId,
			});
		}
		if (options.templateid) {
			this.setData({
				templateid: options.templateid,
			});
		}
		if (options.new_screenitemId) {
			this.setData({
				new_screenitemId: options.new_screenitemId
			})
		}
		if (options.ownerid) {
			this.setData({
				ownerid: options.ownerid
			})

		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	SaveLeadAccount: function() {

		var that = this;
		wx.showLoading({
			title: '正在添加...',
			mask: true
		})
		var lead = this.data.lead;
		lead.ownerid = this.data.ownerid;
		var scobj = this.data.SCOBJ;
		lead.new_gender = this.data.genderIndex + 1;
		scobj.lead = lead;
		scobj.customer = {
			new_scid: this.data.screenId,
			new_screenteamid: this.data.new_screenteamid,
			new_scitemid: this.data.new_screenitemId
		}
		console.log(scobj);
		screenservice.CreateSQXXCJ(scobj).then(function(success) {
			console.log(success);
			that.setData({
				SCOBJ: {},
				lead: {
					new_patientsource: 3,
				},
				IsShowtip: false
			})
			wx.hideLoading();
			if (success.data.resultState == 1) {
				wx.showToast({
					title: '添加成功',
					icon: 'success',
					duration: 2000
				});
			} else if (success.data.resultState == 0) {
				wx.showToast({
					title: '添加失败',
					icon: 'none',
					duration: 2000
				});
			}
		}).catch(function(ex) {
			console.log(ex);
			wx.hideLoading();
			wx.showToast({
				title: '添加失败',
				icon: 'none',
				duration: 2000
			});
		})
	},
	openOrclose: function(e) {
		var flag = this.data.isOpen;
		//console.log(flag);
		if (!flag) {
			this.setData({
				isOpen: true,
				opName: '收起'
			})
		} else {
			this.setData({
				isOpen: false,
				opName: '更多联系方式'
			})
		}

	},
	bindBirthDateChange: function(e) {
		this.setData({
			birthDate: e.detail.value
		});

	},
	checkPhoneAndIDCardAndName: function(obj) {
		var mobile = obj.new_phone;
		var mobile2 = obj.new_mobile;
		var idcard = obj.new_citizenid;
		var qq = obj.new_qq;
		var age = obj.new_age;
		var name = obj.lastname;
		var bingyin = obj.new_disease
		var mobilereg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
		var idreg =
			/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
		var qqreg = /[1-9][0-9]{4,}/
		if (!name || name == '') {
			wx.showToast({
				title: '请填写名字',
				icon: 'none',
				duration: 1500
			})
			return false;
		}

		if (!bingyin || bingyin == '') {
			wx.showToast({
				title: '请选择病种',
				icon: 'none',
				duration: 1500
			})
			return false;
		}

		if (age != '') {
			if (age > 150) {
				wx.showToast({
					title: '请填写正确的年龄',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}

		if (mobile) {
			if (!mobilereg.test(mobile)) {
				wx.showToast({
					title: '请填写正确的手机号码',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}

		if (idcard) {
			if (!idreg.test(idcard)) {
				wx.showToast({
					title: '请填写正确的身份证号码',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}

		if (mobile2) {
			if (!mobilereg.test(mobile2)) {
				wx.showToast({
					title: '请填写正确的手机号码',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}

		if (qq) {
			if (!qqreg.test(qq)) {
				wx.showToast({
					title: '请填写正确的qq号码',
					icon: 'none',
					duration: 1500
				})
				return false;
			}
		}


		return true;

	}
})
